#include "setting.h"
#include "QDebug"

Q_GLOBAL_STATIC(Setting, instance)

Setting::Setting()
{
    setting = new QSettings("./TLBB.ini", QSettings::IniFormat);
    setting->setIniCodec("utf-8");
}

Setting::~Setting()
{
    if(setting != nullptr){
        qDebug()<< "Setting析构函数";
        delete setting;
        setting = nullptr;
    }
}

Setting* Setting::getInstance()
{
    return instance();
}

void Setting::setRegisterCode(QString registerCode)
{
    setting->setValue(REGISTER_CODE, QVariant(registerCode));
}

QString Setting::getRegisterCode()
{
    return setting->value(REGISTER_CODE).toString();
}

void Setting::setOutHellCity(QString cityName)
{
    setting->setValue(HELL_CITY, QVariant(cityName));
}

//获取复活城市
QString Setting::getOutHellCity()
{
    QString city = setting->value(HELL_CITY).toString();
    if(city.isNull() || city.isEmpty()){
        return HELL_LY;
    }
    return city;
}

QString Setting::getBloodKey()
{
    return setting->value(BLOOD_KEY, "").toString();
}

QString Setting::getSkill()
{
      return setting->value(SKILL, "").toString();
}

QString Setting::getSkill2()
{
      return setting->value(SKILL2, "").toString();
}

void Setting::setBloodKey(QString key)
{
    setting->setValue(BLOOD_KEY, QVariant(key));
}

QString Setting::getMagicKey()
{
    return setting->value(MAGIC_KEY, "").toString();
}

void Setting::setMagicKey(QString key)
{
    setting->setValue(MAGIC_KEY, QVariant(key));
}

void Setting::setSkill(QString key)
{
    setting->setValue(SKILL, QVariant(key));
}

void Setting::setSkill2(QString key)
{
    setting->setValue(SKILL2, QVariant(key));
}

void Setting::setAccount(QString account)
{
    setting->setValue(GAME_ACCOUNT,account);
}

QString Setting::getAccount()
{
    return setting->value(GAME_ACCOUNT).toString();
}

void Setting::setPassword(QString password)
{
    QByteArray baPw = password.toUtf8();
    for (int i = 0;i<baPw.size();i++)
    {
        baPw[i] = baPw[i] + 3;//加密User1的密码
    }
    password = baPw;
    setting->setValue(GAME_PASSWORD,password);
}

QString Setting::getPassword()
{
    QString strPw = setting->value(GAME_PASSWORD).toString();//从配置文件中读取账户User1的密码
    QByteArray baPw = strPw.toUtf8();
    for (int i = 0;i<baPw.size();i++)
    {
        baPw[i] = baPw[i] - 3;//解密User1的密码
    }
    strPw = baPw;
    return strPw;
}

void Setting::setRunShopJson(QString runShopJson)
{
    setting->setIniCodec("utf-8");
    setting->setValue(RUN_SHOP_JSON,runShopJson);
}

QString Setting::getRunShopJson()
{
    setting->setIniCodec("utf-8");
    return setting->value(RUN_SHOP_JSON).toString();
}

void Setting::setRoleBeanJson(QString roleJson)
{
    setting->setIniCodec("utf-8");
    setting->setValue(ROLE_JSON,roleJson);
}

QString Setting::getRoleBeanJson()
{
    setting->setIniCodec("utf-8");
    return setting->value(ROLE_JSON).toString();
}

void Setting::setKuoZhangJson(QString json)
{
    setting->setIniCodec("utf-8");
    setting->setValue(KUO_ZHANG_JSON,json);
}

QString Setting::getKuoZhangJson()
{
    setting->setIniCodec("utf-8");
    return setting->value(KUO_ZHANG_JSON).toString();
}

void Setting::setZhongZhiJson(QString json)
{
    setting->setIniCodec("utf-8");
    setting->setValue(ZHONG_ZHI_JSON,json);
}

QString Setting::getZhongZhiJson()
{
    setting->setIniCodec("utf-8");
    return setting->value(ZHONG_ZHI_JSON).toString();
}


void Setting::setImgDiscern(float discern)
{
    setting->setValue(IMG_DISCERN,discern);
}

float Setting::getImgDiscern()
{
    float result = setting->value(IMG_DISCERN).toFloat();
    if(result < 0.8f){
        return 0.95f;
    }
    return result;
}

QString Setting::getSpeed()
{
    return setting->value(SPEED_FUNC).toString();
}

void Setting::setSpeed(QString speed)
{
    setting->setValue(SPEED_FUNC, speed);
}
